package cc.mrbird;

import cc.mrbird.demo.config.WebConfig4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

/**
 * @author: chenhs
 * @date: Created in 16:27 2020/3/21
 **/
@SpringBootApplication
public class AutoconfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(AutoconfigApplication.class, args);


        /**
         * 返回 IOC 容器，使用注解配置，传入配置类; 此处WebConfig中扫描的包com.example.demo不能包含DemoApplication;
         * 否则会重复扫描报出异常。
         */
        ApplicationContext context = new AnnotationConfigApplicationContext(WebConfig4.class);
        System.out.println("容器创建完毕！");
//		User user = context.getBean(User.class);
//		System.out.println(user);

        // 查看 User 这个类在 Spring 容器中叫啥玩意
//		String[] beanNames = context.getBeanNamesForType(User.class);
//		Arrays.stream(beanNames).forEach(System.out::println);

        // 查看基于注解的 IOC容器中所有组件名称
        String[] beanNames = context.getBeanDefinitionNames();
        Arrays.stream(beanNames).forEach(System.out::println);

//		Object user1 = context.getBean("user");
//		Object user2 = context.getBean("user");
//		System.out.println(user1 == user2);


        Object cherry = context.getBean("cherryFactoryBean");
        System.out.println(cherry.getClass());

        Object cherryFactoryBean = context.getBean("&cherryFactoryBean");
        System.out.println(cherryFactoryBean.getClass());

//        ConfigurableApplicationContext context1 = new SpringApplicationBuilder(DemoApplication.class)
//                .web(WebApplicationType.NONE)
//                .profiles("java8")
//                .run(args);
//        CalculateService service = context1.getBean(CalculateService.class);
//        System.out.println("求和结果：" + service.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

    }

}
